package org.theSeed.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.theSeed.base.pojo.dto.CommonKeyValue;
import org.theSeed.base.pojo.ro.PageRo;
import org.theSeed.base.pojo.vo.R;
import org.theSeed.pojo.po.SysRole;
import org.theSeed.pojo.ro.EditRoleRo;
import org.theSeed.service.SysRoleService;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * 角色管理
 */
@RestController
@RequestMapping("sysRole")
public class SysRoleController {
    @Autowired
    public SysRoleService sysRoleService;

    /**
     * 角色列表
     * @return
     */
    @GetMapping( "listRole")
    public R<Page<List<SysRole>>> listRole(PageRo pageRo){
        return R.success(sysRoleService.listRole(pageRo));
    }

    /**
     * 角色详情
     * @param id
     * @return
     */
    @GetMapping( "getRoleDetail/{id}")
    public R<SysRole> getRoleDetail(@PathVariable String id){
        return R.success(sysRoleService.getRoleDetail(id));
    }

    /**
     * 编辑角色
     * @return
     */
    @PostMapping( "editRole")
    public R editRole(@RequestBody EditRoleRo editRoleRo){
        sysRoleService.editRole(editRoleRo);
        return R.success();
    }

    /**
     * 启用停用角色
     * @return
     */
    @PostMapping( "enableRole")
    public R enableRole(@RequestBody @Valid CommonKeyValue<String,String> enableRo){
        sysRoleService.enableRole(enableRo);
        return R.success();
    }

    /**
     * 删除角色
     * @return
     */
    @DeleteMapping( "deleteRole/{id}")
    public R deleteRole(@PathVariable String id){
        sysRoleService.deleteRole(id);
        return R.success();
    }

    /**
     * 角色授权资源
     * @return
     */
    @PostMapping("authResource")
    public R authResource(@RequestBody @Validated CommonKeyValue<String, Set<String>> authResourceRo){
        sysRoleService.authResource(authResourceRo.getKey(),authResourceRo.getValue());
        return R.success();
    }
}
